package com.tcm.pay.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lindongcheng on 14/10/28.
 */
public class DateUtils {

    public static Date getSetoffDay(int days) {
        return getSetoffDay(new Date(), days);
    }

    public static Date getSetoffDayTime(int days, int mins) {
        return getSetoffDayTime(new Date(), days, mins);
    }

    public static Date getSetoffDay(Date aDate, int days) {
        return new Date(aDate.getTime() + days * 24 * 60 * 60 * 1000l);
    }

    public static Date getSetoffDayTime(Date date, int days, int mins) {
        return new Date(date.getTime() + days * 24 * 60 * 60 * 1000l + mins * 60 * 1000l);
    }

    public static String formatDate(Date date) {
        return formatDate(date, "yyyyMMdd");
    }

    public static String formateIsoDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd'T'HH:mm:ss");
    }

    public static String formatDate(Date date, String formatStr) {
        SimpleDateFormat dateFormater1 = new SimpleDateFormat(formatStr);
        return dateFormater1.format(date);
    }

    public static Date getByOffset(int timeType, int timeValue) {
        switch (timeType) {
            case 0: //x天前
                return getSetoffDay(-timeValue);
            case 1: //x时前
                return getSetoffDayTime(new Date(), 0, -60 * timeValue);
            case 2: //xmin前
                return getSetoffDayTime(0, -timeValue);
            case 4: //x 秒前
                return new Date(new Date().getTime() - timeValue);
            default:
                throw new RuntimeException("无此时间类型");
        }
    }

    /**
     * 将字串转成日期和时间，字串格式: yyyy-MM-dd
     *
     * @param string
     * @return
     */
    public static Date toDate(String string) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.parse(string);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 将字串转成日期和时间，字串格式: yyyyMMddHHmmss
     *
     * @param string
     * @return
     */
    public static Date toDateTime(String string) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            return formatter.parse(string);
        } catch (Exception ex) {
            return null;
        }
    }

    public static Date toDateTime(String string, String formatterString) {
        try {
            DateFormat formatter = new SimpleDateFormat(formatterString);
            return formatter.parse(string);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 获取本月 开始 和 结束的 时间
     *
     * @return date[] 0:begin 1:end
     */
    public static Date[] getCurMonthBeginEndDate() {
        Date nowdate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        /* 设置为当前时间 */
        cal.setTime(nowdate);
        /* 当前日期月份 + x */
        cal.add(Calendar.MONTH, 0);
        // 得到前一个月的第一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        String beginS = sdf.format(cal.getTime()) + "000000";
        // 得到前一个月的最后一天
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endS = sdf.format(cal.getTime()) + "235959";

        return new Date[]{toDateTime(beginS), toDateTime(endS)};
    }

    /**
     * 下次时间
     * 次数 l
     * 时间 date
     *
     * @return
     */
    public static Date nextNotifyTime(Long l, Date date) {
        long t = date.getTime();
        t = t + l * 1200000;
        return new Date(Long.valueOf(t));
    }

    /**
     * 昨天开始时间
     *
     * @return
     */
    public static Date startOfbeforeday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.DAY_OF_MONTH, -1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 前天结束时间
     *
     * @return
     */
    public static Date endForBeforeDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.DAY_OF_MONTH, -1);

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 该开始时间
     *
     * @return
     */
    public static Date startForThisTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

/*        calendar.add(Calendar.DAY_OF_MONTH, -1);*/

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 该天结束时间
     *
     * @return
     */
    public static Date endForThisTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        /*calendar.add(Calendar.DAY_OF_MONTH, -1);*/

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /**
     * 计算两日期之间相差的天数
     *
     * @param startDay 开始日期 (日期格式yyyyMMddHHmmss)
     * @param endDay   结束日期 (日期格式yyyyMMddHHmmss)
     * @return
     */
    public static int getDaysBetween(Date startDay, Date endDay) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startDay);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endDay);
        // 确保cal1在cal2之前
        if (cal1.after(cal2)) {
            Calendar swap = cal1;
            cal1 = cal2;
            cal2 = swap;
        }
        int days = cal2.get(Calendar.DAY_OF_YEAR) - cal1.get(Calendar.DAY_OF_YEAR);
        int y2 = cal2.get(Calendar.YEAR);
        if (cal1.get(Calendar.YEAR) != y2) {
            cal1 = (Calendar) cal1.clone();
            do {
                //得到当年的实际天数
                days += cal1.getActualMaximum(Calendar.DAY_OF_YEAR);
                cal1.add(Calendar.YEAR, 1);
            } while (cal1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * 　　* 判断时间是否在时间段内 *
     * <p>
     * 　　* @param date
     * <p>
     * 　　* 当前时间 yyyy-MM-dd HH:mm:ss
     * <p>
     * 　　* @param strDateBegin
     * <p>
     * 　　* 开始时间 00:00:00
     * <p>
     * 　　* @param strDateEnd
     * <p>
     * 　　* 结束时间 00:05:00
     * <p>
     * 　　* @return
     * <p>
     */

    public static boolean isInDate(Date date, String strDateBegin,
                                   String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        int strDateS = Integer.parseInt(strDate.substring(17, 19));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM
                    && strDateS >= strDateBeginS && strDateS <= strDateEndS) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH
                    && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数

            } else return strDateH >= strDateBeginH && strDateH == strDateEndH
                    && strDateM == strDateEndM && strDateS <= strDateEndS;
        } else {
            return false;
        }

    }

    /**
     * 当前时间
     *
     * @return
     */
    public static Date currentTime() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 所给时间是否为当天
     *
     * @return
     */
    public static boolean isToday(Date date) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            return diffDay == 0;
        }
        return false;
    }

}
